﻿using Sasc.PV.BLL;
using Sasc.PV.BLL.ZhuDing;
using Sasc.PV.Common;
using Sasc.PV.DAL;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Sasc.PV.Web.Admin.Zhuding
{
    public partial class ShaoJieYunZhuanModify : PVGongXuAdminPage<ZhuDingCheJianManagement, ZhuDingShaoJieGongXu, ZhuDingGongXuTarget>
    {
        #region PageLoad
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!HasPermission(GongXuQuanXianLeiXing.MODIFY))
                MessageBox.ShowAndRedirect(this, "对不起，没有权限访问该页面。", "PenTu");

            if (!IsPostBack)
            {
                var model = new PvZDShaoJieCollection();
                model = model.GetModel(int.Parse(Request.QueryString["ID"].ToString()));

                GanGuoZhuiZongDanMatchTextBoxCheckBox.Checked = model.GanGuoZhuiZongDanMatch;
                JianCheDanZhuiZongDanOKCheckBox.Checked = model.JianCheDanZhuiZongDanOK;
                JiaReBangOKCheckBox.Checked = model.JiaReBangOK;
                ShaoJieLuBianHaoTextBox.Text = model.ShaoJieLuBianHao;
                ShaoJieLuYunXingJianChaRenTextBox.Text = model.YunXingQianJiaChaRen;
                BeiZhuTextBox.Text = model.BeiZhu;
                GongYiMingChengTextBox.Text = model.GongYiMingCheng;
                CreatedByLabel.Text = model.CreatedBy;
                CreatedDateLabel.Text = model.CreatedDate.Format();
                LastUpdatedByLabel.Text = model.LastUpdatedBy;
                LastUpdatedDateLabel.Text = model.LastUpdatedDate.Format();

                DataBindToShaoJieGridview();
                DataBindToShaoJieYunXingGridView();
            }
        }
        #endregion

        #region GetQuanXianValue
        protected override string GetQuanXianValue()
        {
            return "104";
        }
        #endregion

        #region Submit Button
        protected void SubmitImageButton_Click(object sender, ImageClickEventArgs e)
        {
            var model = new PvZDShaoJieCollection();
            model = model.GetModel(int.Parse(Request.QueryString["ID"].ToString()));
            if (model == null)
            {
                MessageBox.ShowAndRedirect(this, "《烧结炉运转记录单》修改成功！", "ShaoJieYunZhuan");
            }

            decimal parseDecimal = 0;
            DateTime parseDateTime = DateTime.Now;

            //BeiZhu = string.Empty,
            //GongYiMingCheng = string.Empty,
            model.GanGuoZhuiZongDanMatch = GanGuoZhuiZongDanMatchTextBoxCheckBox.Checked;
            model.JianCheDanZhuiZongDanOK = JianCheDanZhuiZongDanOKCheckBox.Checked;
            model.JiaReBangOK = JiaReBangOKCheckBox.Checked;
            model.ShaoJieLuBianHao = ShaoJieLuBianHaoTextBox.Text;
            model.YunXingQianJiaChaRen = ShaoJieLuYunXingJianChaRenTextBox.Text;
            model.BeiZhu = BeiZhuTextBox.Text;
            model.GongYiMingCheng = GongYiMingChengTextBox.Text;
            model.LastUpdatedBy = LoginUserName;
            model.LastUpdatedDate = DateTime.Now;

            model.Update();

            MessageBox.ShowAndRedirect(this, "《烧结炉运转记录单》修改成功！", "ShaoJieYunZhuan");
        }
        #endregion
        
        #region 坩埚烧结

        protected void DataBindToShaoJieGridview(bool withNewRow = false)
        {
            var MyModel = new PvZDShaoJie();
            var dataSet = MyModel.GetList(" ShaoJieCollectionID=" + Request.QueryString["ID"].ToString());
            if (withNewRow)
            {
                var newRow = dataSet.Tables[0].NewRow();
                newRow["ID"] = 0;
                newRow["GanGuoXuHao"] = dataSet.Tables[0].Rows.Count + 1;
                newRow["GanGuoBianHao"] = "";
                newRow["JinLuDate"] = DbContants.DefaultDateTime;
                newRow["JinLuCaoZuoRen"] = "";
                newRow["QiDongDate"] = DbContants.DefaultDateTime;
                newRow["QiDongCaoZuoRen"] = "";
                newRow["ChuLuDate"] = DbContants.DefaultDateTime;
                newRow["ChuLuCaoZuoRen"] = "";
                newRow["JianYanJiLuBiaoBanHao"] = "";
                dataSet.Tables[0].Rows.Add(newRow);
                ShaoJieGVData.EditIndex = dataSet.Tables[0].Rows.Count - 1;
            }
            ShaoJieGVData.DataSource = dataSet;
            ShaoJieGVData.DataBind();
        }

        protected void ShaoJieGVData_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            PublicMethod.GridViewRowDataBound(e);
            if (e.Row.RowType == DataControlRowType.DataRow && (e.Row.RowState & DataControlRowState.Edit) == DataControlRowState.Edit)
            {
                var dataRow = e.Row.DataItem as DataRowView;
                WebUtils.SetGridViewCellValue<TextBox>(e.Row, "GanGuoXuHaoTextBox", c => c.Text, dataRow["GanGuoXuHao"]);
                WebUtils.SetGridViewCellValue<TextBox>(e.Row, "GanGuoBianHaoTextBox", c => c.Text, dataRow["GanGuoBianHao"]);
                WebUtils.SetGridViewCellValue<TextBox>(e.Row, "JinLuDateTextBox", c => c.Text, ConvertHelper.ChangeType<DateTime>(dataRow["JinLuDate"]).Format());
                WebUtils.SetGridViewCellValue<TextBox>(e.Row, "JinLuCaoZuoRenTextBox", c => c.Text, dataRow["JinLuCaoZuoRen"]);
                WebUtils.SetGridViewCellValue<TextBox>(e.Row, "QiDongDateTextBox", c => c.Text, ConvertHelper.ChangeType<DateTime>(dataRow["QiDongDate"]).Format());
                WebUtils.SetGridViewCellValue<TextBox>(e.Row, "QiDongCaoZuoRenTextBox", c => c.Text, dataRow["QiDongCaoZuoRen"]);
                WebUtils.SetGridViewCellValue<TextBox>(e.Row, "ChuLuDateTextBox", c => c.Text, ConvertHelper.ChangeType<DateTime>(dataRow["ChuLuDate"]).Format());
                WebUtils.SetGridViewCellValue<TextBox>(e.Row, "ChuLuCaoZuoRenTextBox", c => c.Text, dataRow["ChuLuCaoZuoRen"]);
                WebUtils.SetGridViewCellValue<TextBox>(e.Row, "JianYanJiLuBiaoBanHaoTextBox", c => c.Text, dataRow["JianYanJiLuBiaoBanHao"]);
            }

            var deleteButton = e.Row.FindControl("DeleteButton");
            if (deleteButton != null && HasPermission(GongXuQuanXianLeiXing.DELETE))
                deleteButton.Visible = true;
        }

        protected void ShaoJieGVData_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            var shaojieCollectionID = int.Parse(Request["ID"]);
            var reason = string.Empty;

            decimal parseDecimal = 0;
            DateTime parseDateTime = DateTime.Now;

            var model = new PvZDShaoJie
            {
                LastUpdatedBy = LoginUserName,
                LastUpdatedDate = DateTime.Now,
                FanGongVersion = 0,
                BeiZhu = "",
                ShaoJieCollectionID = shaojieCollectionID,
                ID = WebUtils.GetGridViewCellValue<HiddenField, int>(ShaoJieGVData, e.RowIndex, "IDHidden", c => c.Value, 0),
                GanGuoXuHao = WebUtils.GetGridViewCellValue<TextBox, int>(ShaoJieGVData, e.RowIndex, "GanGuoXuHaoTextBox", c => c.Text, 0),
                GanGuoBianHao = WebUtils.GetGridViewCellValue<TextBox, string>(ShaoJieGVData, e.RowIndex, "GanGuoBianHaoTextBox", c => c.Text, ""),
                JinLuDate = WebUtils.GetGridViewCellValue<TextBox, DateTime>(ShaoJieGVData, e.RowIndex, "JinLuDateTextBox", c => c.Text, DbContants.DefaultDateTime),
                JinLuCaoZuoRen = WebUtils.GetGridViewCellValue<TextBox, string>(ShaoJieGVData, e.RowIndex, "JinLuCaoZuoRenTextBox", c => c.Text, ""),
                QiDongDate = WebUtils.GetGridViewCellValue<TextBox, DateTime>(ShaoJieGVData, e.RowIndex, "QiDongDateTextBox", c => c.Text, DbContants.DefaultDateTime),
                QiDongCaoZuoRen = WebUtils.GetGridViewCellValue<TextBox, string>(ShaoJieGVData, e.RowIndex, "QiDongCaoZuoRenTextBox", c => c.Text, ""),
                ChuLuDate = WebUtils.GetGridViewCellValue<TextBox, DateTime>(ShaoJieGVData, e.RowIndex, "ChuLuDateTextBox", c => c.Text, DbContants.DefaultDateTime),
                ChuLuCaoZuoRen = WebUtils.GetGridViewCellValue<TextBox, string>(ShaoJieGVData, e.RowIndex, "ChuLuCaoZuoRenTextBox", c => c.Text, ""),
                JianYanJiLuBiaoBanHao = WebUtils.GetGridViewCellValue<TextBox, string>(ShaoJieGVData, e.RowIndex, "JianYanJiLuBiaoBanHaoTextBox", c => c.Text, "")
            };
            if (model.ID == 0)
            {
                model.CreatedBy = LoginUserName;
                model.CreatedDate = DateTime.Now; 
            }

            var valid = true;
            if (model.GanGuoXuHao <= 0)
            {
                MessageBox.Show(this, "序号不能为空。");
                valid = false;
            }

            if (valid && string.IsNullOrEmpty(model.GanGuoBianHao))
            {
                MessageBox.Show(this, "坩埚编号不能为空。");
                valid = false;
            }

            if (valid)
            {
                var target = model.CreateTarget();

                if (model.ID == 0)
                {
                    if (CheJianGongXu.CanProcess(model, target, out reason))
                    {
                        CheJianGongXu.AddGongXuRec(model, target);

                        MessageBox.Show(this, "坩埚烧结记录创建成功！");
                        ShaoJieGVData.EditIndex = -1;
                        DataBindToShaoJieGridview();
                    }
                    else
                    {
                        MessageBox.Show(this, reason);
                    }
                }
                else
                {
                    CheJianGongXu.UpdateGongXuRec(model, target);
                    WriteRiZhi(string.Format("修改坩埚烧结记录：{0}", model.ID));
                    MessageBox.Show(this, "坩埚烧结记录修改成功！");
                    ShaoJieGVData.EditIndex = -1;
                    DataBindToShaoJieGridview();
                }
            }
        }

        protected void ShaoJieGVData_RowEditing(object sender, GridViewEditEventArgs e)
        {
            ShaoJieGVData.EditIndex = e.NewEditIndex;
            var gridViewRow = ShaoJieGVData.Rows[e.NewEditIndex];
            var commandCell = gridViewRow.Cells[gridViewRow.Cells.Count - 1];
            DataBindToShaoJieGridview();
        }

        protected void ShaoJieGVData_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            ShaoJieGVData.EditIndex = -1;
            DataBindToShaoJieGridview();
        }

        protected void AddShaoJieGanGuoJiLuButton_Click(object sender, EventArgs e)
        {
            DataBindToShaoJieGridview(true);
        }

        protected void ShaoJieGVData_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            var shaoJieID = (long)e.Keys[0];
            if (shaoJieID <= 0)
            {
                e.Cancel = true;
                MessageBox.Show(this, "不存在该坩埚烧结记录。");
                DataBindToShaoJieGridview();
            }
            else
            {
                CheJianGongXu.DeleteGongXuRecList(shaoJieID.ToString());
                MessageBox.Show(this, "删除坩埚烧结记录成功。");
                DataBindToShaoJieGridview();
            }
        }
        #endregion

        #region 烧结炉运行事件

        protected void DataBindToShaoJieYunXingGridView(bool withNewRow = false)
        {
            var MyModel = new PvZDShaoJieYunXing();
            var dataSet = MyModel.GetList(" ShaoJieCollectionID=" + Request.QueryString["ID"].ToString());
            if (withNewRow)
            {
                var newRow = dataSet.Tables[0].NewRow();
                newRow["ID"] = 0;
                newRow["ShaoJieYuXingDate"] = DbContants.DefaultDateTime;
                newRow["AXiangDianYa"] = (decimal)0.0;
                newRow["AXiangDianLiu"] = (decimal)0.0;
                newRow["BXiangDianYa"] = (decimal)0.0;
                newRow["BXiangDianLiu"] = (decimal)0.0;
                newRow["CXiangDianYa"] = (decimal)0.0;
                newRow["CXiangDianLiu"] = (decimal)0.0;
                newRow["YiHaoTongDaoTemp"] = (decimal)0.0;
                newRow["ErHaoTongDaoTemp"] = (decimal)0.0;
                newRow["SanHaoTongDaoTemp"] = (decimal)0.0;
                dataSet.Tables[0].Rows.Add(newRow);
                ShaoJieYunXingGridView.EditIndex = dataSet.Tables[0].Rows.Count - 1;
            }
            ShaoJieYunXingGridView.DataSource = dataSet;
            ShaoJieYunXingGridView.DataBind();
        }

        protected void AddShaoJieYunXingButtonJiLuButton_Click(object sender, EventArgs e)
        {
            DataBindToShaoJieYunXingGridView(true);

        }
        protected void ShaoJieYunXingGridView_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            ShaoJieYunXingGridView.EditIndex = -1;
            DataBindToShaoJieYunXingGridView();
        }

        protected void ShaoJieYunXingGridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            PublicMethod.GridViewRowDataBound(e);
            if (e.Row.RowType == DataControlRowType.DataRow && (e.Row.RowState & DataControlRowState.Edit) == DataControlRowState.Edit)
            {
                var dataRow = e.Row.DataItem as DataRowView;
                WebUtils.SetGridViewCellValue<TextBox>(e.Row, "ShaoJieYuXingDateTextBox", c => c.Text, ConvertHelper.ChangeType<DateTime>(dataRow["ShaoJieYuXingDate"]).Format());
                WebUtils.SetGridViewCellValue<TextBox>(e.Row, "AXiangDianLiuTextBox", c => c.Text, ConvertHelper.ChangeType<decimal>(dataRow["AXiangDianLiu"]));
                WebUtils.SetGridViewCellValue<TextBox>(e.Row, "BXiangDianLiuTextBox", c => c.Text, ConvertHelper.ChangeType<decimal>(dataRow["BXiangDianLiu"]));
                WebUtils.SetGridViewCellValue<TextBox>(e.Row, "CXiangDianLiuTextBox", c => c.Text, ConvertHelper.ChangeType<decimal>(dataRow["CXiangDianLiu"]));
                WebUtils.SetGridViewCellValue<TextBox>(e.Row, "AXiangDianYaTextBox", c => c.Text, ConvertHelper.ChangeType<decimal>(dataRow["AXiangDianYa"]));
                WebUtils.SetGridViewCellValue<TextBox>(e.Row, "BXiangDianYaTextBox", c => c.Text, ConvertHelper.ChangeType<decimal>(dataRow["BXiangDianYa"]));
                WebUtils.SetGridViewCellValue<TextBox>(e.Row, "CXiangDianYaTextBox", c => c.Text, ConvertHelper.ChangeType<decimal>(dataRow["CXiangDianYa"]));
                WebUtils.SetGridViewCellValue<TextBox>(e.Row, "YiHaoTongDaoTempTextBox", c => c.Text, ConvertHelper.ChangeType<decimal>(dataRow["YiHaoTongDaoTemp"]));
                WebUtils.SetGridViewCellValue<TextBox>(e.Row, "ErHaoTongDaoTempTextBox", c => c.Text, ConvertHelper.ChangeType<decimal>(dataRow["ErHaoTongDaoTemp"]));
                WebUtils.SetGridViewCellValue<TextBox>(e.Row, "SanHaoTongDaoTempTextBox", c => c.Text, ConvertHelper.ChangeType<decimal>(dataRow["SanHaoTongDaoTemp"]));
            }

            var deleteButton = e.Row.FindControl("DeleteButton");
            if (deleteButton != null && HasPermission(GongXuQuanXianLeiXing.DELETE))
                deleteButton.Visible = true;
        }

        protected void ShaoJieYunXingGridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            var shaoJieYunXingID = (long)e.Keys[0];
            if (shaoJieYunXingID <= 0)
            {
                e.Cancel = true;
                MessageBox.Show(this, "不存在该烧结炉运行记录。");
                DataBindToShaoJieYunXingGridView();
            }
            else
            {
                new PvZDShaoJieYunXing().Delete(shaoJieYunXingID);
                WriteRiZhi(string.Format("删除烧结炉运行记录：{0}", shaoJieYunXingID));
                MessageBox.Show(this, "删除烧结炉运行记录成功。");
                DataBindToShaoJieYunXingGridView();
            }
        }

        protected void ShaoJieYunXingGridView_RowEditing(object sender, GridViewEditEventArgs e)
        {
            ShaoJieYunXingGridView.EditIndex = e.NewEditIndex;
            DataBindToShaoJieYunXingGridView();
        }

        protected void ShaoJieYunXingGridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            var shaojieCollectionID = int.Parse(Request["ID"]);
            var reason = string.Empty;

            decimal parseDecimal = 0;
            DateTime parseDateTime = DateTime.Now;

            var model = new PvZDShaoJieYunXing
            {
                LastUpdatedBy = LoginUserName,
                LastUpdatedDate = DateTime.Now,
                ShaoJieCollectionID = shaojieCollectionID,
                ID = WebUtils.GetGridViewCellValue<HiddenField, int>(ShaoJieYunXingGridView, e.RowIndex, "IDHidden", c => c.Value, 0),
                ShaoJieYuXingDate = WebUtils.GetGridViewCellValue<TextBox, DateTime>(ShaoJieYunXingGridView, e.RowIndex, "ShaoJieYuXingDateTextBox", c => c.Text, DbContants.DefaultDateTime),
                AXiangDianLiu = WebUtils.GetGridViewCellValue<TextBox, decimal>(ShaoJieYunXingGridView, e.RowIndex, "AXiangDianLiuTextBox", c => c.Text, 0),
                BXiangDianLiu = WebUtils.GetGridViewCellValue<TextBox, decimal>(ShaoJieYunXingGridView, e.RowIndex, "BXiangDianLiuTextBox", c => c.Text, 0),
                CXiangDianLiu = WebUtils.GetGridViewCellValue<TextBox, decimal>(ShaoJieYunXingGridView, e.RowIndex, "CXiangDianLiuTextBox", c => c.Text, 0),
                AXiangDianYa = WebUtils.GetGridViewCellValue<TextBox, decimal>(ShaoJieYunXingGridView, e.RowIndex, "AXiangDianYaTextBox", c => c.Text, 0),
                BXiangDianYa = WebUtils.GetGridViewCellValue<TextBox, decimal>(ShaoJieYunXingGridView, e.RowIndex, "BXiangDianYaTextBox", c => c.Text, 0),
                CXiangDianYa = WebUtils.GetGridViewCellValue<TextBox, decimal>(ShaoJieYunXingGridView, e.RowIndex, "CXiangDianYaTextBox", c => c.Text, 0),
                YiHaoTongDaoTemp = WebUtils.GetGridViewCellValue<TextBox, decimal>(ShaoJieYunXingGridView, e.RowIndex, "YiHaoTongDaoTempTextBox", c => c.Text, 0),
                ErHaoTongDaoTemp = WebUtils.GetGridViewCellValue<TextBox, decimal>(ShaoJieYunXingGridView, e.RowIndex, "ErHaoTongDaoTempTextBox", c => c.Text, 0),
                SanHaoTongDaoTemp = WebUtils.GetGridViewCellValue<TextBox, decimal>(ShaoJieYunXingGridView, e.RowIndex, "SanHaoTongDaoTempTextBox", c => c.Text, 0),
            };

            var valid = true;
            if (model.ShaoJieYuXingDate== DbContants.DefaultDateTime)
            {
                MessageBox.Show(this, "时间不能为空。");
                valid = false;
            } 

            if (valid)
            { 
                if (model.ID == 0)
                {
                    if (model.Add()>0)
                    {
                        WriteRiZhi("增加烧结炉运行记录");
                        MessageBox.Show(this, "烧结炉运行记录创建成功！");
                        ShaoJieYunXingGridView.EditIndex = -1;
                        DataBindToShaoJieYunXingGridView();
                    }
                    else
                    {
                        MessageBox.Show(this, "烧结炉运行记录创建失败！");
                    }
                }
                else
                {
                    model.Update();
                    WriteRiZhi(string.Format("修改烧结炉运行记录：{0}", model.ID));
                    MessageBox.Show(this, "烧结炉运行记录修改成功！");
                    ShaoJieYunXingGridView.EditIndex = -1;
                    DataBindToShaoJieYunXingGridView();
                }
            }
        }
        #endregion

    }
}